package com.xmb.wechat.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.baserecyclerview.BaseRecyclerViewManager;
import com.xmb.wechat.definterface.CommonIAndSListener;
import com.xmb.wechat.entity.IntegerAndStringEntity;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/26 10:00
 */
public class CommonListDialog extends Dialog {

    private CommonListDialogDelegate mDelegate;

    public CommonListDialog(Context context, CommonIAndSListener listener) {
        super(context, R.style.PopupDialog);
        initView(context, listener);
    }

    private void initView(Context context, CommonIAndSListener listener) {
        View view = LayoutInflater.from(context).inflate(R.layout.common_list_dialog, null, false);
        mDelegate = new CommonListDialogDelegate(context, view.findViewById(R.id.recyclerView));
        mDelegate.getAdapter().setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                IntegerAndStringEntity entity = (IntegerAndStringEntity) mDelegate.getData().get(position);
                if (entity == null)
                    return;
                if (listener != null)
                    listener.onChoose(entity.getType(), entity.getValue());
                dismiss();
            }
        });
        setContentView(view);
        WindowManager.LayoutParams lp = Objects.requireNonNull(getWindow()).getAttributes();
        lp.dimAmount = 0.5f;
        lp.width = ScreenUtils.getScreenWidth() * 4 / 5;
        getWindow().setAttributes(lp);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    }

    public void setData(ArrayList arrayList) {
        if (arrayList.size() < 1)
            return;
        mDelegate.clearData();
        mDelegate.addData(arrayList);
    }

    private class CommonListDialogDelegate extends BaseRecyclerViewManager {
        public CommonListDialogDelegate(Context context, RecyclerView view) {
            super(context, view);
        }

        @Override
        public BaseQuickAdapter initAdapter() {
            return new BaseQuickAdapter<IntegerAndStringEntity, BaseViewHolder>(R.layout.item_common_list_dialog, new ArrayList<>()) {
                @Override
                protected void convert(BaseViewHolder helper, IntegerAndStringEntity item) {
                    helper.setText(R.id.tv_, TextUtils.isEmpty(item.getValue()) ? "" : item.getValue());
                }
            };
        }
    }
}
